package com.ts_xiaoa.ts_android_library.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.LinearLayout
import androidx.core.view.LayoutInflaterCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.ts_xiaoa.ts_android_library.BaseActivity
import com.ts_xiaoa.ts_android_library.R
import com.ts_xiaoa.ts_android_library.databinding.ActivityWidgetBinding
import com.ts_xiaoa.ts_android_library.widget.indicator.TabIndicatorActivity
import com.ts_xiaoa.ts_base.utils.startActivityOrForResult
import com.ts_xiaoa.ts_glide.loadCenterCropImage
import com.ts_xiaoa.ts_ui.helper.LayoutConfig
import kotlinx.coroutines.delay
import me.samlss.broccoli.Broccoli
import me.samlss.broccoli.BroccoliGradientDrawable
import me.samlss.broccoli.PlaceholderParameter

/**
 * create by ts_xiaoA on 2020-08-28 20:03
 * email：443502578@qq.com
 * desc：
 */
class WidgetActivity : BaseActivity() {

    private val binding by lazy { rootBinding as ActivityWidgetBinding }
    val broccoli = Broccoli()
    val myAdapter = MyAdapter()

    override fun getLayoutConfig(): LayoutConfig {
        return LayoutConfig(R.layout.activity_widget)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        LayoutInflaterCompat.setFactory2(layoutInflater, object : LayoutInflater.Factory2 {
            override fun onCreateView(
                parent: View?,
                name: String?,
                context: Context,
                attrs: AttributeSet
            ): View? {
                val view = delegate.createView(parent, name, context, attrs)//调用系统的方法
                println("onCreateView56=====$name===========${view}")
                LinearLayout(context, attrs)
                if (view != null) {
                    broccoli.addPlaceholder(
                        PlaceholderParameter.Builder()
                            .setView(view)
                            .setDrawable(
                                BroccoliGradientDrawable(
                                    Color.parseColor("#DDDDDD"),
                                    Color.parseColor("#ffffff"),
                                    20f, 1000,
                                    LinearInterpolator()
                                )
                            ).build()
                    )
                }
                return view
            }

            override fun onCreateView(
                name: String?,
                context: Context?,
                attrs: AttributeSet?
            ): View? {
                return null
            }
        })
        super.onCreate(savedInstanceState)
    }


    override fun initView(savedInstanceState: Bundle?) {

        binding.rvData.apply {
            layoutManager = LinearLayoutManager(this@WidgetActivity)
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            adapter = myAdapter
        }
        broccoli.show()
    }

    @SuppressLint("SetTextI18n")
    override fun onInit(savedInstanceState: Bundle?) {
        request {
            delay(3000)
            val testImageUrl =
                "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=954496120,1621506021&fm=11&gp=0.jpg"
            binding.ivTest.loadCenterCropImage(testImageUrl, 20)
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.getData().add("")
            myAdapter.notifyDataSetChanged()
//            broccoli.removePlaceholder(binding.ivTest)
//            binding.btnIndicator.text = "lalalalala"
            broccoli.clearAllPlaceholders()
//            broccoli.clearPlaceholder(binding.btnIndicator)
//            binding.ivTest.loadCenterCropImage(testImageUrl)

        }
    }

    override fun initEvent(savedInstanceState: Bundle?) {
        //TabLayout 指示器演示
        binding.btnIndicator.setOnClickListener {
            startActivityOrForResult<TabIndicatorActivity>()
        }
    }

}